import subprocess
import os
from os import path

PWD = os.getcwd().replace(' ', "\ ") # 将文件路径做空格处理


def pullNewCode():
    print('\n=====================拉取最新代码命令=====================\n')
    gitPullCmd = 'git pull'
    process = subprocess.Popen(gitPullCmd, shell=True)
    process.wait()
    print(gitPullCmd)
    print('\n=====================拉取最新代码命令=====================\n')
    
def exportIpa(name_archive, path_ipa, path_plist):
    if (len(path_plist) == 0):
        print('\n=====================打包支持的plist文件未做到!!!=====================\n')
        return

    print('\n=====================导出命令=====================\n')
    ipaCmd ='xcodebuild -exportArchive -archivePath '+name_archive+'.xcarchive -exportPath '+path_ipa+' -exportOptionsPlist '+path_plist
    process = subprocess.Popen(ipaCmd, shell=True)
    process.wait()
    print(ipaCmd)
    print('\n=====================导出命令=====================\n')

    ipaReturnCode = process.returncode      
    if ipaReturnCode != 0:
        print('ipa导出失败')
    else:
        print('ipa导出成功')

def cleanArchiveFile(name_archive):

    print('\n=====================清除命令=====================\n')
    cleanCmd = 'rm -r '+name_archive+'.xcarchive'
    process = subprocess.Popen(cleanCmd, shell=True)
    process.wait()
    print(cleanCmd)
    print('\n=====================清除命令=====================\n')

    cleanReturnCode = process.returncode      
    if cleanReturnCode != 0:
        print('archive清除失败')
    else:
        print('archive清除成功')

def renameIpa(path_ipa, name_project, name_ipa):

    old_ipa_Path = os.getcwd()+'/'+path_ipa+'/'+name_project+'.ipa'
    print("老路径:" + old_ipa_Path)
    new_ipa_Path = os.getcwd()+'/'+path_ipa+'/'+name_ipa+'.ipa'
    print("新路径:" + new_ipa_Path)
    
    os.rename(old_ipa_Path, new_ipa_Path)
    print('重命名 ipa')
    
    print('\n=====================打开命令=====================\n')
    openCmd = 'open ' + os.path.dirname(new_ipa_Path)
    print(openCmd)
    process = subprocess.Popen(openCmd, shell=True)
    process.wait()
    print('\n=====================打开命令=====================\n')
    
def exportdSYM(name_project, path_ipa):
    print('\n=====================导出dSYM命令=====================\n')
    dSYMCmd = 'cp -a '+name_project+'.xcarchive/dSYMs/'+name_project+'.app.dSYM ./'+path_ipa
    process = subprocess.Popen(dSYMCmd, shell=True)
    process.wait()
    print(dSYMCmd)
    print('\n=====================导出dSYM命令=====================\n')

    iReturnCode = process.returncode
    if iReturnCode != 0:
        print('dSYM导出失败')
    else:
        print('dSYM导出成功')
        
def main():

    parent_path = os.path.abspath(os.path.dirname(PWD))
    print("获取父类路径:" + parent_path)

    #---------------------------------配置工程信息>>>>>>---------------------------------
    # 工程名
#    name_project = 'IntelligentLight'
    name_project = parent_path.split("/")[-1] # 为了自动获取
    print("工程名:" + name_project)
    name_projectT = name_project.replace(' ', "\ ") # 将文件名做空格处理
    # 包类型  Release Debug
    name_config = 'Release'
    # scheme名称（一般无需更改，支持多个scheme）
    name_scheme = name_projectT

    #---------------------------------<<<<<<配置工程信息---------------------------------
    
    pullNewCode()
    

    # 工程路径
    project_Path = parent_path
    print("工程路径:" + project_Path)
    
    # 工程编译路径
    build_project_Path = project_Path+"/"+name_projectT+".xcworkspace"
    print("工程编译路径:" + build_project_Path)
    
    # archive 名称
    name_archive = name_projectT
    # ipa 名称
    name_ipa = name_projectT+'_'+name_config
    # archive 导出路径
    xcarchive_Path="./"+name_archive
    print("archive 导出路径:" + xcarchive_Path)
    
    # 编译生成xcarchive
    buildWorkspace(build_project_Path, name_scheme, name_config, xcarchive_Path)
    
    # ipa 导出
    path_ipa="ExportIPA"
    path_plist = PWD+'/ExportOptions.plist'
    print("ipa 导出路径:" + path_plist)
    exportIpa(name_projectT, path_ipa, path_plist)
    
    # dSYM 导出
    exportdSYM(name_projectT, path_ipa)
    
    # 清除 Archive
    cleanArchiveFile(xcarchive_Path)
    
    # 重命名 ipa
    renameIpa(path_ipa, name_project, name_ipa)

def buildWorkspace(build_project_Path, name_scheme, name_config, xcarchive_Path):
    print('\n=====================编译命令=====================\n')
    xcarchiveCmd = 'xcodebuild -workspace '+build_project_Path+' -scheme '+name_scheme+' -configuration '+name_config+' -sdk iphoneos archive -archivePath '+xcarchive_Path

    print(xcarchiveCmd)
    print('\n=====================编译命令=====================\n')

    process = subprocess.Popen(xcarchiveCmd, shell=True)
    process.wait()

    xcarchiveReturnCode = process.returncode
    if xcarchiveReturnCode != 0:
        print('acrchive失败')
    else:
        print('acrchive成功')


if __name__ == '__main__':
    
    # 类似证书配置。在执行之前，请先手动打包获取 ExportOptions.plist 文件即可
#    build_iphoneCmd = 'python3 build_iphone.py'
#    process = subprocess.Popen(build_iphoneCmd, shell=True)
#    process.wait()
    main()
    

